#!/usr/bin/env python

# !/usr/bin/env python
# -*- coding: utf-8 -*-

import re
import urllib2
from dict.desease_dict import deseases
from dict.drugs_dict import drugs
from dict.cell_dict import cells
from dict.bacteria_dict import bacterias
from dict.enzime_dict import enzime
from dict.fungi_dict import fungis
from dict.scince_dict import scince
from dict.virus_dict import virus
from dict.human_part_dict import humans_part
from dict.measure_dict import measures
from dict.operation_dict import operations
from str_analyzer import StrAnalyzer
import time

__author__ = 'gogo'

__version__ = '0.0.1'

max_chars = None

start_time = time.time()

sa = StrAnalyzer()
cnt = 0


all_dict = [bacterias, cells, deseases, drugs,
            enzime, fungis, humans_part, measures,
            operations, scince, virus
            ]

for my_dict in all_dict:
    for w in my_dict:
        # print w
        # print w, sa.detect_object(w)
        try:
            if sa.detect_object(w)[1] is not None:
                request = urllib2.urlopen('http://en.wikipedia.org/wiki/' + w.capitalize())
            #request = urllib2.urlopen('http://en.wikipedia.org/wiki/Ramipril')
                content = request.read()
            #match = re.findall(r'<p>(.*)</p>', remove_non_ascii(content))
                match = re.findall(r'<p>.*<b>.*</b>.*</p>', content,re.IGNORECASE)
                if match:
                    # print match
                    for p in match:
                        ptext = re.sub('<[^<]+?>', '', p)
                        if len(ptext) > len(w[0]) and (w.lower() in ptext.lower()):
                            print "%s -> %s" % (w, ptext[:max_chars])
                            print "-" * 80
                            break
        except:
            pass

end_time = time.time()

print cnt, end_time - start_time